package com.zcx.qshop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.view.swipe.SwipeMenu;
import com.zcx.helper.view.swipe.SwipeMenuListView;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.adapter.CollectionAdapter;
import com.zcx.qshop.conn.JsonCollectAsyGet;
import com.zcx.qshop.conn.JsonCollectdeleteAsyGet;
import com.zcx.qshop.entity.Good;
import com.zcx.qshop.menu.DeleteMenuCreator;
import com.zcx.qshop.view.TitleView;

/**
 * Created by Administrator on 2015/8/22 0022.
 */
public class CollectionActivity extends QSActivity {

    @BoundView(R.id.title_view) private TitleView titleView;

    @BoundView(R.id.collection_list_view) private SwipeMenuListView swipeMenuListView;

    private CollectionAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_collection);

        titleView.showStyle(TitleView.Style.FULL);

        titleView.showType(TitleView.Type.BACK_TWO, TitleView.Type.NAME_ONE);

        titleView.setTitleName("我的收藏");

        titleView.setOnTitleItemClickListener(new TitleView.OnTitleItemClickListener() {

            @Override
            public void onBackClick() {

                finish();

            }

        });

        swipeMenuListView.setMenuCreator(new DeleteMenuCreator(this));

        swipeMenuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Good good = new Good();

                good.pid = adapter.getItem(position).gid;

                startVerifyActivity(CommodityDetailActivity.class, new Intent().putExtra("Good", good));

            }

        });

        swipeMenuListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {

            @Override
            public void onMenuItemClick(final int i, SwipeMenu swipeMenu, int i1) {

                new JsonCollectdeleteAsyGet(QSApplication.QSPreferences.readUid(), adapter.getItem(i).cid, new AsyCallBack() {

                    @Override
                    public void onSuccess(String toast, int type, Object o) throws Exception {

                        adapter.remove(adapter.getItem(i));

                    }

                }).execute(CollectionActivity.this);

            }

        });

        new JsonCollectAsyGet(QSApplication.QSPreferences.readUid(), new AsyCallBack<JsonCollectAsyGet.Info>(){

            @Override
            public void onSuccess(String toast, int type, JsonCollectAsyGet.Info info) throws Exception {

                swipeMenuListView.setAdapter(adapter = new CollectionAdapter(CollectionActivity.this, info.collects));

            }

        }).execute(this);

    }

}
